
import 'package:lai_zhan/common/util/LocalStorage.dart';
import 'package:flutter_redux/flutter_redux.dart';
import 'package:redux/redux.dart';
import 'package:lai_zhan/common/redux/UserRedux.dart';
import 'package:lai_zhan/common/redux/AppState.dart';
import 'package:lai_zhan/common/config/Config.dart';
import 'dart:convert';
import 'package:lai_zhan/common/model/userResult/user_result.dart';
import 'DataResult.dart';
import 'package:lai_zhan/common/net/HttpManager.dart';
import 'package:lai_zhan/common/net/ResultData.dart';

class UserDao {
  //初始化user
  static initUserInfo(Store<AppState> store) async {
    String token = await LocalStorage.get(Config.TOKEN_KEY);
    DataResult res = await getUserinfoLocal();
    if(res.result&&res.data!=null){
      store.dispatch(UpdateUserAction(res.data));
    }
    return new DataResult(res.data, (res.result&&(token!=null)));
  }
  //获取本地user信息
  static getUserinfoLocal() async {
    var userText = await LocalStorage.get(Config.USER_INFO);
    if (userText != null) {
      var userMap = json.decode(userText);
      UserResult user = UserResult.fromJson(userMap);
      return new DataResult(user, true);
    }else {
      return new DataResult(null, false);
    }
  }
  //登陆
  static login(String account,String pwd,Store<AppState> store) async {
    //待定
    Object params = {"var":"17855833071","password":"123456"};
    ResultData re = await HttpManager.netPost("/user/login", params);
    print(re.result);
    if(re.result) {
      await LocalStorage.save(Config.USER_INFO, re.data);
      print(re.data);
      print(json.decode(re.data));
      print(json.decode(re.data)["msg"]);
      UserResult user = UserResult.fromJson(json.decode(re.data)["msg"]);
      print(user.hpicurl);
      DataResult dataResult = new DataResult(user, re.result);
      store.dispatch(UpdateUserAction(user));
      return dataResult;
    }
    return new DataResult(null, false);
  }
  //注册
  static register(String account,String pwd,Store<AppState> store) async {
    //待定
    return new DataResult(null, false);
  }
  //信息更新
  static updateUserInfo({String gender,String descr,String email,String uname}) async {
    //----
    return new DataResult(null, false);
  }
}
